using System;
using System.Linq;
using System.Collections.Generic;
using DevExpress.CodeRush.StructuralParser;

namespace CR.StyleCop.Naming {
    public class FieldNamesMustNotUseHungarianNotation : BaseStyleCopIssueSearcher<IFieldElement, IVariableDeclarationStatement> {
        // TODO (fen): This should be in a Config dialog so that the user can input data.
        private static string[] HungarianNotations = new[] { "b", "c", "dw", "f", "n", "i", "fp", "db", "p", "rg", "sz", "u32", "st", "fn", "rgfp", "aul", "hwnd", "lpsz" };

        public override IEnumerable<StyleCopIssue> FindFirstIssues(IEnumerable<IElement> elements) {
            return elements.Where(e => IsHungarian(e.Name))
                .Select(e => new StyleCopIssue {
                    Element = e,
                    IssueDescription = GetIssueDescription(e)
                });
        }

        private static bool IsHungarian(string name) {
            var hungarian = HungarianNotations.Where(notation => {
                if (!name.StartsWith(notation))
                    return false;
                if (char.IsLower(name[notation.Length]))
                    return false;

                return true;
            });

            return hungarian.Count() > 0;
        }

        private static string GetIssueDescription(IElement element) {
            if (element is IFieldElement)
                return StyleCop.Resource.FieldNamesMustNotUseHungarianNotationFieldIssueDescription;
            else
                return StyleCop.Resource.FieldNamesMustNotUseHungarianNotationVariableIssueDescription;
        }
    }
}